package net.ddns.xcyyds.kungfu.procedures;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.TickEvent;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.TextComponent;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;

import javax.annotation.Nullable;

@Mod.EventBusSubscriber
public class MessageOutProcedure {
	@SubscribeEvent
	public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			execute(event, event.player.level, event.player);
		}
	}

	public static void execute(LevelAccessor world, Entity entity) {
		execute(null, world, entity);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, Entity entity) {
		if (entity == null)
			return;
		if (entity.getPersistentData().getDouble("oneHandJian") < KungFuModVariables.MapVariables.get(world).sld1
				&& KungFuModVariables.MapVariables.get(world).sld1 < (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian) {
			ShuLianDuAddNeiLiProcedure.execute(entity);
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(
						new TextComponent("\u4F60\u7684\u5355\u624B\u5251\u672F\u719F\u7EC3\u5EA6\u7B49\u7EA7\u8FBE\u5230\u4E862\u7EA7\uFF01"),
						(false));
		}
		if (entity.getPersistentData().getDouble("oneHandJian") < KungFuModVariables.MapVariables.get(world).sld2
				&& KungFuModVariables.MapVariables.get(world).sld2 < (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian) {
			ShuLianDuAddNeiLiProcedure.execute(entity);
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(
						new TextComponent("\u4F60\u7684\u5355\u624B\u5251\u672F\u719F\u7EC3\u5EA6\u7B49\u7EA7\u8FBE\u5230\u4E863\u7EA7\uFF01"),
						(false));
		}
		if (entity.getPersistentData().getDouble("oneHandJian") < KungFuModVariables.MapVariables.get(world).sld3
				&& KungFuModVariables.MapVariables.get(world).sld3 < (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian) {
			ShuLianDuAddNeiLiProcedure.execute(entity);
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(
						new TextComponent("\u4F60\u7684\u5355\u624B\u5251\u672F\u719F\u7EC3\u5EA6\u7B49\u7EA7\u8FBE\u5230\u4E864\u7EA7\uFF01"),
						(false));
		}
		if (entity.getPersistentData().getDouble("oneHandJian") < KungFuModVariables.MapVariables.get(world).sld4
				&& KungFuModVariables.MapVariables.get(world).sld4 < (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian) {
			ShuLianDuAddNeiLiProcedure.execute(entity);
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(
						new TextComponent("\u4F60\u7684\u5355\u624B\u5251\u672F\u719F\u7EC3\u5EA6\u7B49\u7EA7\u8FBE\u5230\u4E865\u7EA7\uFF01"),
						(false));
		}
		entity.getPersistentData().putDouble("oneHandJian", ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian));
	}
}
